<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no"/>
    <title>修改头像</title>
    <link rel="stylesheet" type="text/css" href="../../css/common.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui-flex.css"/>
    <style>
        body {
            font-size: 14px;
        }

        footer.aui-nav {
            z-index: 99999;
        }

        header.aui-bar {
            background: #fff;
        }

        .aui-title {
            color: #000;
        }

        .aui-nav .aui-bar-tab .active .aui-iconfont, .aui-nav .aui-bar-tab .active p {
            color: #327afa !important;
        }

        .active {
            color: #327afa !important;
        }

        .aui-tab-nav li.active {
            color: #327afa;
            border-bottom: 2px #327afa solid;
        }

        .aui-bar .aui-iconfont {
            color: #000;
        }

        .send {
            /*font-size: 12px !important;*/
        }

        .name {
            width: 80% !important;
            margin-left: 10%;
            border: none !important;
            border-bottom: 1px solid #ccc !important;
            background: transparent !important;
            margin-top: 15px;
        }
        .send{

        }
    </style>
</head>
<body>
<header class="aui-bar aui-bar-nav aui-border-b">
    <div class="aui-pull-left aui-iconfont aui-icon-left icon-left-coler" tapmode onclick="closeFrame()"></div>
    <div class="aui-title">
        修改头像
    </div>
    <div class="aui-btn aui-btn-success aui-pull-right send " onclick="save()"
         style="height: 37px;margin-top: 4px;line-height: 37px;margin-right: 15px;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;">
        保存
    </div>
    <!--<div class="" tapmode onclick="send()" style="color:#327afa;">-->
    <!--保存</div>-->
</header>

</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/mui.js"></script>
<script type="text/javascript" src="../../script/jquery-1.11.3.js"></script>
<script type="text/javascript">
    var name = "", localImageUrl, FNImageClip,iosHeader;
    apiready = function () {
        api.parseTapmode();
        localImageUrl = api.pageParam.localImageUrl;


        var header = $api.dom('header');
        $api.fixIos7Bar(header);
        FNImageClip = api.require('FNImageClip');
        imageClipOpen();
    }
    function imageClipOpen() {
        FNImageClip.open({
            rect: {
                x: 0,
                y: 45+(api.systemType=="ios"?21.0:0.0),
                w: api.winWidth,
                h: api.winHeight-45
            },
            srcPath: localImageUrl,
            style: {
                mask: 'rgba(0,0,0,0.3)',
                clip: {
                    w: 200,
                    h: 200,
                    x:  api.winWidth/2-100,
                    y:(api.winHeight-45)/2-100,
                    borderColor: '#0f0',
                    borderWidth: 1,
                    appearance: 'rectangle'
                }
            },
            fixedOn: api.frameName,
            mode:'image'
        }, function (ret, err) {
        });
    }

    function save() {
        $(".send").attr("onclick","");
        $(".send").text("更新中");
        $(".send").css({"background":"#d9d9d9","color":"#bababa"})
        FNImageClip.save({
            destPath: 'fs://imageClip/result.png',
            copyToAlbum: false,
            quality: 1
        }, function(ret, err) {
            if (ret) {

                var img = new Image();
                img.src = ret.destPath;
                img.onload = function () {
                    var w = this.width, h = this.height, scale = w / h;
                    w = 640 || w;
                    h = w / scale;
                    //生成canvas
                    var canvas = document.createElement('canvas');
                    var ctx = canvas.getContext('2d');
                    $api.attr(canvas, 'width', '' + w + '');
                    $api.attr(canvas, 'height', '' + h + '');
                    ctx.drawImage(this, 0, 0, w, h);
                    imgBase64 = canvas.toDataURL('image/jpeg');
                    getStorageAll();
                    //上传图片
                    postData();
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }
    function postData() {
        showLoading();
        var data = {};
        data['user_id'] = user_id;
        data['base64'] = imgBase64;
        var url = 'Mall/imageUpload';
        ajaxRequest(url, 'post', data, function (ret, err) {
            if (ret.status == 1) {
                var newHeaderPic=ret.msg;
                var data2 = {};
                data2['userName'] = user_name;
                data2['head_portrait'] = ret.msg;
                var url = 'User/memberUpdate';
                ajaxRequest(url, 'post', data2, function (ret, err) {
                    if (ret.status == 1) {
                        $api.setStorage('head_pic', '' + newHeaderPic + '');
                        api.sendEvent({
                            name:"modifyInfo",
                            extra:{
                                modify:true
                            }
                        })
                        api.toast({
                            msg: '更新成功！',
                            duration: 2000,
                            location: 'bottom'
                        })
                        closeFrame();
                        hideLoading();
                    } else {
                        closeFrame();
                        hideLoading();
                        api.toast({
                            msg: '更新出错,请重试！',
                            duration: 2000,
                            location: 'bottom'
                        })
                    }
                })
            } else {

                closeFrame();
                api.toast({
                    msg: ret.msg,
                    duration: 2000,
                    location: 'bottom'
                })
            }
        })
    }
</script>
</html>
